
package cnhy.salary.controller;

import cn.hutool.core.date.DateUtil;
import cnhy.salary.domain.DailyWage;
import cnhy.salary.domain.PostCfg;
import cnhy.salary.domain.dto.EmployeeDto;
import cnhy.salary.domain.dto.lineDailyWageDto;
import cnhy.salary.domain.dto.lineDeptMonthlyWageDto;
import cnhy.salary.domain.dto.lineMonthlyWageDto;
import cnhy.salary.service.LineSvc;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

import lombok.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import cnhy.salary.domain.ProductionLine;
//import cnhy.salary.service.ProductionLineService;
import ling.common.*;
import ling.common.domain.*;

/** 产线 控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-5-23
 */
@Api(tags = "产线 功能接口")
@RestController
@RequestMapping("/salary/ProductionLine")
public class ProductionLineController extends EQueryController<ProductionLine>{



    @ApiOperation("详情")
    @PreAuthorize("@ps.any(@pay.productionLine.query)")
    @GetMapping(value = "/{id}")
    public Result<ProductionLine> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }

    @ApiOperation("分页列表")
    @PreAuthorize("@ps.any(@pay.productionLine.list)")
    @GetMapping("list")
    public ResultRows<ProductionLine> list(ProductionLine dto, PageDomain pageRequest) {

        return queryPage(dto, pageRequest);

    }
    @ApiOperation("分页列表扩展")
    @PreAuthorize("@ps.any(@pay.productionLine.list)")
    @GetMapping("listExt")
    public ResultRows<ProductionLine> listExt(ProductionLine dto, PageDomain pageRequest) {

        var row = queryPage(dto, pageRequest);
        var ids = row.getRows().stream().map(p -> p.getId()).toArray();
        if(ids.length<1)
            return ResultRows.toEmpty(null);
        var ext = queryHelper.CreateItems(sql1).in("p.id", ids).resultPkg().queryMap();
        ResultRows extRe = new ResultRows<>(row.getTotal(), ext);
        return extRe;
    }

    /** 产线统计
     *
     */
    String sql1="select * from pay_production_line p left join ( SELECT sum(pcount) empCount,count(1) postCount,max(line_id) line_id,max(line_name) line_name,max(line_leader) line_leader,max(line_phone) line_phone from ( select count(1) pcount,max(post) post,max(line_id) line_id,max(line_name) line_name,max(line_leader) line_leader,max(line_phone) line_phone from v_employee_info GROUP BY line_id,post ) b GROUP BY line_id ) n on p.id=n.line_id where ";
    @ApiOperation("新增")
    @PreAuthorize("@ps.any(@pay.productionLine.add)")
    @Log(title = "产线", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated @RequestBody ProductionLine entity){
        var old=queryHelper.entityQuery(ProductionLine.class).where(p->p.name().eq(entity.getName())).firstOrNull();
        if(old!=null)
            return Result.bad("名称重复");
        return super.add(entity);
    }

    @ApiOperation("删除")
    @PreAuthorize("@ps.any(@pay.productionLine.remove)")
    @Log(title = "产线", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }

    @ApiOperation("编辑")
    @PreAuthorize("@ps.any(@pay.productionLine.edit)")
    @Log(title = "产线", businessType = BusinessType.UPDATE)
    @PutMapping
    public  Result edit(@Validated @RequestBody ProductionLine entity){

        var old=queryHelper.entityQuery(ProductionLine.class)
                .where(p -> p.id().ne(entity.getId()))
                .where(p->p.name().eq(entity.getName())).firstOrNull();
        if(old!=null)
            return Result.bad("名称重复");
        return super.edit(entity);
    }

    @ApiOperation("导出")
    @PreAuthorize("@ps.any(@pay.productionLine.export)")
    @Log(title = "产线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductionLine entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }

    @Resource
    LineSvc lineSvc;
    @ApiOperation("产线岗位统计")
    @PreAuthorize("@ps.any(@pay.productionLine.list)")
    @GetMapping("linePosts")
    public ResultRows<PostCfg> linePosts(Long lineId, PageSort pageSort){
       return lineSvc.linePosts(lineId,pageSort);
    }
    @ApiOperation(value = "人员统计",notes = "需要产线id:lineId")
    @PreAuthorize("@ps.any(@pay.productionLine.list)")
    @GetMapping("lineEmplList")
    public ResultRows<EmployeeDto> lineEmplList(EmployeeDto dto, PageSort pageSort){
        return lineSvc.lineEmplList(dto,pageSort);
    }
    @ApiOperation(value = "日工资统计导出",notes = "日薪资返回： time,wage估算工资,ratioBaseFinal实际工资")
    @PreAuthorize("@ps.any(@pay.productionLine.export)")
    @PostMapping("exportLineDailyWage")
    public void exportLineDailyWage(Long lineId, PageSort pageSort,HttpServletResponse response) {
        var re = lineSvc.lineDailyWage(lineId, pageSort);
        var util = new ExcelUtil(lineDailyWageDto.class);
        util.exportExcel(response, re.getData().stream().map(p->{
            var dto=new lineDailyWageDto();
            dto.setTime(p.getTime());
            dto.setWage(p.getWage());
            dto.setRatioBaseFinal(p.getRatioBaseFinal());
            return  dto;
        }).collect(Collectors.toList()), "日工资统计");
    }
    @ApiOperation(value = "日工资统计",notes = "日薪资返回： time,wage估算工资,ratioBaseFinal实际工资")
    @PreAuthorize("@ps.any(@pay.productionLine.list)")
    @GetMapping("lineDailyWage")
    public ResultRows<DailyWage> lineDailyWage(Long lineId, PageSort pageSort){
        return lineSvc.lineDailyWage(lineId,pageSort);
    }

    @ApiOperation(value = "月工资统计",notes = "产线月工资  line_id,line_name产线名称,time 月份,wage_total 月工资")
    @PreAuthorize("@ps.any(@pay.productionLine.list)")
    @GetMapping("lineMonthlyWage")
    public ResultRows<lineMonthlyWageDto> lineMonthlyWage(Long lineId, PageSort pageSort){
        return lineSvc.lineMonthlyWage(lineId,pageSort);
    }
    @ApiOperation(value = "月工资统计导出",notes = "产线月工资  line_id,line_name产线名称,time 月份,wage_total 月工资")
    @PreAuthorize("@ps.any(@pay.productionLine.list)")
    @PostMapping("exportLineMonthlyWage")
    public void exportLineMonthlyWage(Long lineId, PageSort pageSort,HttpServletResponse response) {
        var data = lineSvc.lineMonthlyWage(lineId, pageSort);
        ExcelUtilEx.exportData(lineMonthlyWageDto.class, data.getData(), response, "月工资统计");
    }

    @ApiOperation(value = "月部门工资统计",notes = "line_id,line_name产线名称,time 月份,wage_total月工资,dept_id,dept_name部门名称")
    @PreAuthorize("@ps.any(@pay.productionLine.list)")
    @GetMapping("lineDeptMonthlyWage")
    public ResultRows<Map> lineDeptMonthlyWage(Long lineId,PageSort pageSort){
        return lineSvc.lineDeptMonthlyWage(lineId,pageSort);
    }
    @ApiOperation("月部门工资统计导出")
    @PreAuthorize("@ps.any(@pay.productionLine.export)")
    @Log(title = "产线", businessType = BusinessType.EXPORT)
    @PostMapping("/exportLineDeptMonthlyWage")
    public void exportLineDeptMonthlyWage(Long lineId,PageSort pageSort,HttpServletResponse response)
    {
        var data= lineSvc.lineDeptMonthlyWage(lineId,pageSort);
        var list=new ArrayList<lineDeptMonthlyWageDto>();
        for (var item : data.getRows())
        {
          var newItem=  ObjectConverter.convert(item, lineDeptMonthlyWageDto.class);
          list.add(newItem);
        }

        var util = new ExcelUtil(lineDeptMonthlyWageDto.class);
        util.exportExcel(response, list,"月部门工资统计");

    }
}